//
//  WBHomeViewController.swift
//  Weibo
//
//  Created by yinbo on 2018/2/27.
//  Copyright © 2018年 yinbo. All rights reserved.
//

import UIKit

//定义全局变量，尽量使用private 修饰,否则到处都可以使用
private let originalCellID="originalCellID"
private let retweetedCellID="retweetedCellID"

class WBHomeViewController: WBBaseViewController {
    lazy var statusList = [String]()
    //列表视图模型
    lazy var listViewModel = WBStatusListViewModel()
    override func loadData() {
        refreshControl?.beginRefreshing()

        listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess,shouldRefresh) in
            self.refreshControl?.endRefreshing()
            self.isPullup = false
            if shouldRefresh == true{
            self.tableView?.reloadData()
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    @objc  func showfriends(){
        let vc = WBDemoViewController()
        vc.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(vc, animated: true)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

//MARK:表格数据源方法
extension WBHomeViewController{
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let vm = listViewModel.statusList[indexPath.row]
        return vm.rowHeigth
        
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let viewModel = listViewModel.statusList[indexPath.row]
        
        let cellID = (viewModel.status.retweeted_status != nil) ? retweetedCellID : originalCellID
        //1.取cell-本身会调用代理方法（如果有)
        //如果没有，找到cell,按照自动布局的规划，找到向下的约束，从而计算动态行高
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! WBStatusCell
        
        cell.viewModel = viewModel
//        cell.statusLabel?.text = listViewModel.statusList[indexPath.row].status.text
//        cell.nameLab.text = listViewModel.statusList[indexPath.row].status.user?.screen_name
        return cell
    }
    
}
//MARK: 设置界面
extension WBHomeViewController{
    //重写父类方法
   
    override func setupTableView() {
        super.setupTableView()
        //没法高亮
        //navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action:#selector(showfriends))
        //Swift 调用OC方法返回instancetype的方法，判断不出是否可选
        //let btn:UIButton = UIButton.cz_textButton("好友", fontSize: 16 , normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
        //btn.addTarget(self, action: #selector(showfriends), for: .touchUpInside)
        //navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fontSize: 16, target: self, action: #selector(showfriends))
//        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        tableView?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: originalCellID)
        tableView?.register(UINib(nibName: "WBStatusRetweetedCell", bundle: nil), forCellReuseIdentifier: retweetedCellID)
        //取消自动行高
//        tableView?.rowHeight = UITableViewAutomaticDimension
        //预估行高
        tableView?.estimatedRowHeight = 300
        tableView?.separatorStyle = .none
        setupNavTitle()
        
    }
    func setupNavTitle(){
//        
//        let button = UIButton.cz_textButton("波波", fontSize: 17, normalColor:  UIColor.darkGray, highlightedColor: UIColor.black)
//        button?.setImage(UIImage(named:"navigationbar_arrow_down"), for: [])
//        button?.setImage(UIImage(named:"navigationbar_arrow_up"), for: .selected)
//        navItem.titleView = button
//        button?.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
        var title = ""
        guard let name = WBNetworkManager.shared.userAccount.screen_name else { return
            
        }
      
        title = WBNetworkManager.shared.userAccount.screen_name! + "  "
       
         let button = WBTitleButton(title: title)
         navItem.titleView = button
         button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
    }
    func clickTitleButton(btn:UIButton){
        btn.isSelected = !btn.isSelected
    }
}
